#!/bin/bash


shellout(){
 echo 
 echo "ERROR $? in $1 commande";
 echo
 #rm -rf "$FAKE_ROOT"
 #"$STARTDIR/kakola.app" "$PKGNAME"
 exit 2;
}

run_cmd(){
    echo -n "  $1" && $1 > /dev/null 2>&1 || shellout "$1" ; echo
}

umask 022
VERSION=`tail -1 ../src/VERSION`
CURRENTUSER=`logname`
WHOAMI=`whoami`

STARTDIR=`pwd`
ARCH=`uname -p`
ARCH=`file "$STARTDIR/../src/kakola.app/Contents/MacOS/kakola" | sed -e "s/.* executable //g"`
BINARY=$STARTDIR/../binary/
ARCH="intel-universal-i386-x86_64"
mkdir -p "$STARTDIR/../build/"
PKGNAME=$STARTDIR/../build/kakola-$ARCH-$VERSION.pkg
DMGNAME=$STARTDIR/../build/kakola-$ARCH-$VERSION.dmg
MPKGNAME32=$STARTDIR/../build/kakola-intel-i386-$VERSION.mpkg
MPKGNAME64=$STARTDIR/../build/kakola-intel-x86_64-$VERSION.mpkg
PACKAGEMAKER="/Developer/usr/bin/packagemaker"
PKG_OSX_TEMPLATE=$STARTDIR/../package-macosx-kakola-templates/
FAKE_ROOT="/var/tmp/package-macosx-kakola-$VERSION/root/"
SHARED="$FAKE_ROOT/Users/Shared/"
APPLICATIONS="$FAKE_ROOT/Applications/"
PROFILES="$FAKE_ROOT/Applications/kakola.app/Contents/Resources/profiles/"


if [ ! "$WHOAMI" = "root" ]
then
    echo "You are running this program with user $WHOAMI"
    echo "You must be root to create the package"
    echo "Try with sudo $0"
    echo
    exit 1
fi


if echo "$STARTDIR" | grep " "
then
    shellout "$STARTDIR contain space, dangerous existing, rename your folder"
fi

echo ""

URL_KAKOLA_TOOLS_I386=http://kakola.googlecode.com/files/kakola-tools-0.6.1-i386.dmg
URL_KAKOLA_TOOLS_X86_64=http://kakola.googlecode.com/files/kakola-tools-0.6.1-x86_64.dmg

mkdir -p "$BINARY"
echo "Downloading file $URL_KAKOLA_TOOLS_I386 into $BINARY"
cd $BINARY || shellout "cd $BINARY"
curl -O -C - $URL_KAKOLA_TOOLS_I386
curl -O -C - $URL_KAKOLA_TOOLS_X86_64

if ! file kakola-tools-0.6.1-x86_64.dmg | grep "VAX COFF executable not stripped"
then
        echo "Creating file binary/kakola-tools-0.6.1-x86_64.dmg"
        rm -f kakola-tools-0.6.1-x86_64.dmg
        cd $STARTDIR/../tools && ./create_package.sh 2>&1 || shellout "cd $STARTDIR/../tools && ./create_package.sh"
        cp ../build/kakola-tools-0.6.1-x86_64.dmg $BINARY
fi


cd $STARTDIR/
sudo -u $CURRENTUSER sed -e 's#appIcon.rc#icons/application-icon.icns#g' ../src/kakola.pro > ../src/kakola.mac.pro

rm -rf "$FAKE_ROOT" "$PKGNAME" > /dev/null 2>&1

echo
cd $STARTDIR
echo "Building kakola project ..."
CMD="sudo -u $CURRENTUSER sh ../src/build.mac.sh"
[ "$1" = "NO_COMPILE" ] || run_cmd "$CMD"

echo
cd $STARTDIR/../src/
echo "Building macdeployqt project ..."
CMD="chown -R $CURRENTUSER:admin macdeployqt"
run_cmd "$CMD"
CMD="cd  macdeployqt/macdeployqt"
run_cmd "$CMD"
CMD="sudo -u $CURRENTUSER qmake -spec macx-g++ -r"
run_cmd "$CMD"
CMD="sudo -u $CURRENTUSER make"
run_cmd "$CMD"
MACDEPLOYQT="macdeployqt/bin/macdeployqt"


echo
cd $STARTDIR/../src/
echo "Running $MACDEPLOYQT"
CMD="sudo -u $CURRENTUSER $MACDEPLOYQT kakola.app -dmg"
run_cmd "$CMD"
rm -f kakola.dmg



echo
echo -n "Creating directory ... $FAKE_ROOT"
rm -rf "$FAKE_ROOT"
CMD="mkdir -p $FAKE_ROOT"
run_cmd "$CMD"

echo
echo "Create directory $APPLICATIONS"
CMD="mkdir -p $APPLICATIONS"
run_cmd "$CMD"

CMD="rm -rf $APPLICATIONS/kakola.app"
run_cmd "$CMD"

CMD="cp -R kakola.app $APPLICATIONS"
run_cmd "$CMD"

CMD="cd $FAKE_ROOT"
run_cmd "$CMD"

CMD="chmod -R 755 $FAKE_ROOT" 
run_cmd "$CMD"

echo
echo "Copy documentations, ressources, translate, ... into package"
CMD="mkdir -p $APPLICATIONS/kakola.app/Contents/Resources/"
run_cmd "$CMD"

CMD="cp -R $STARTDIR/../documentations $APPLICATIONS/kakola.app/Contents/Resources/"
run_cmd "$CMD"

CMD="mkdir $APPLICATIONS/kakola.app/Contents/Resources/documentations/translate/"
run_cmd "$CMD"

CMD="cp $STARTDIR/../src/kakola_*.qm $APPLICATIONS/kakola.app/Contents/Resources/documentations/translate/"
run_cmd "$CMD"

CMD="cp $STARTDIR/../src/VERSION $APPLICATIONS/kakola.app/Contents/Resources/" 
run_cmd "$CMD"


CMD="mkdir -p ${FAKE_ROOT}/usr/bin/"
run_cmd "$CMD"

cp $STARTDIR/../src/{addDatePrefixToFilename.sh,copyDateInformation.sh,addDatePrefixToAllFilenameInThisDirectory.sh,find_duplicate_file_v2.pl} ${FAKE_ROOT}/usr/bin/
CMD="cp $STARTDIR/../src/{addDatePrefixToFilename.sh,copyDateInformation.sh,addDatePrefixToAllFilenameInThisDirectory.sh,find_duplicate_file_v2.pl} ${FAKE_ROOT}/usr/bin/"
#run_cmd "$CMD"
echo $CMD

echo
echo "Creating directory ..."
CMD="mkdir -p $PROFILES"
run_cmd "$CMD"

CMD="cp $STARTDIR/../profiles/mac/*.profile $PROFILES"
run_cmd "$CMD"

CMD="mkdir -p $APPLICATIONS/kakola.app/Contents/Resources/Binary/"
run_cmd "$CMD"

CMD="cp $STARTDIR/../src/icons/background.tiff $APPLICATIONS/kakola.app/Contents/Resources/"
run_cmd "$CMD"


CMD="cp $STARTDIR/../src/icons/background.tiff $PKG_OSX_TEMPLATE/resources/"
run_cmd "$CMD"




echo
echo "Downloading ffmpeg file from http://kakola.googlecode.com/files/ffmpeg "

[ ! -d "$STARTDIR/../binary" ] && mkdir "$STARTDIR/../binary"
CMD="cd $STARTDIR/../binary"
run_cmd "$CMD"

echo
echo "Changing owner and permission"
CMD="chown -R root:wheel $APPLICATIONS"
run_cmd "$CMD"

CMD="chown -R root:wheel $APPLICATIONS/kakola.app"
run_cmd "$CMD"

CMD="chmod -R 775 $APPLICATIONS/kakola.app"
run_cmd "$CMD"


rm -rf $PKGNAME
echo;
echo "Creating package $PKGNAME"
CMD="$PACKAGEMAKER -build -p $PKGNAME -f $FAKE_ROOT -ds  \
 -r $PKG_OSX_TEMPLATE/resources/ \
 -s $PKG_OSX_TEMPLATE/resources/ \
 -i $PKG_OSX_TEMPLATE/Info.plist \
 -d $PKG_OSX_TEMPLATE/Description.plist"
run_cmd "$CMD"

CMD="defaults delete $PKG_OSX_TEMPLATE/Info.plist IFPkgPathMappings"
echo "  $CMD" && $CMD > /dev/null 2>&1 

CMD="cp $PKG_OSX_TEMPLATE/Info.plist $PKGNAME/Contents/Info.plist"
run_cmd "$CMD"


CMD="cp $STARTDIR/../tools/templates/Resources/background.tiff $PKGNAME/Contents/Resources/"
run_cmd "$CMD"


CMD="rm -f $PKGNAME/Contents/Resources/TokenDefinitions.plist"
run_cmd "$CMD"


echo "Creating DMG file …."
rm -rf "$STARTDIR/../build/kakola-x86_64-$VERSION.dmg"
CMD="hdiutil create $DMGNAME -srcfolder $PKGNAME -ov"
run_cmd "$CMD"


echo
echo
echo "Files bellow  have been generated :"
echo
echo "Directory : $STARTDIR/../build/"
echo
cd "$STARTDIR/../build/"
du -sh "$DMGNAME"
echo
echo

exit 0





rm -rf $STARTDIR/metapackage-macos/packages/
mkdir -p $STARTDIR/metapackage-macos/packages/
CMD="cp -r -p $PKGNAME $STARTDIR/metapackage-macos/packages/"
run_cmd "$CMD"

#cd $STARTDIR/../binary/
#CMD="curl -O -C - http://kakola.googlecode.com/files/mplayer-devel-31034-x86_64.dmg"
#echo "  $CMD" && $CMD > /dev/null 2>&1
#CMD="curl -O -C - http://kakola.googlecode.com/files/ffmpeg-0.6.1-x86_64.dmg"
#echo "  $CMD" && $CMD > /dev/null 2>&1
#CMD="curl -O -C - http://kakola.googlecode.com/files/ffmpeg-mplayer-i386.dmg"
#echo "  $CMD" && $CMD > /dev/null 2>&1


#MPLAYER=`hdiutil attach $STARTDIR/binary/mplayer-devel-31034-x86_64.dmg | grep Apple_HFS | tail -n1 | awk '{print $3}' `
#FFMPEG=`hdiutil attach $STARTDIR/binary/ffmpeg-0.6.1-x86_64.dmg | grep Apple_HFS | tail -n1 | awk '{print $3}'`
#FFMPEGB=`hdiutil attach $STARTDIR/binary/ffmpeg-mplayer-i386.dmg | grep Apple_HFS | tail -n1 | awk '{print $3}'`
KAKOLATOOLSI386=`hdiutil attach $STARTDIR/../binary/kakola-tools-0.6.1-i386.dmg| grep Apple_HFS | tail -n1 | awk '{print $3}'`
KAKOLATOOLSX86_64=`hdiutil attach $STARTDIR/../binary/kakola-tools-0.6.1-x86_64.dmg| grep Apple_HFS | tail -n1 | awk '{print $3}'`

CMD="cd $STARTDIR"
run_cmd "$CMD"
CMD="rm -rf $MPKGNAME32 $MPKGNAME64"
run_cmd "$CMD"
CMD="mkdir -p $MPKGNAME32/Contents/Resources/ $MPKGNAME64/Contents/Resources/"
run_cmd "$CMD"

CMD="cp -r -p $PKGNAME $MPKGNAME32/Contents/Resources/"
run_cmd "$CMD"
CMD="cp -r -p $PKGNAME $MPKGNAME64/Contents/Resources/"
run_cmd "$CMD"

CMD="cp -r -p $KAKOLATOOLSI386/kakola-tools-0.6.1-i386.pkg $MPKGNAME32/Contents/Resources/"
run_cmd "$CMD"
CMD="cp -r -p $KAKOLATOOLSX86_64/kakola-tools-0.6.1-x86_64.pkg $MPKGNAME64/Contents/Resources/"
run_cmd "$CMD"

CMD="umount $KAKOLATOOLSI386"
run_cmd "$CMD"
CMD="umount $KAKOLATOOLSX86_64"
run_cmd "$CMD"

CMD="cp -r -p $STARTDIR/metapackage-macos/Info.plist $MPKGNAME32/Contents/"
run_cmd "$CMD"
CMD="cp -r -p $STARTDIR/metapackage-macos/Info.plist $MPKGNAME64/Contents/"
run_cmd "$CMD"
CMD="cp -r -p $PKGNAME/Contents/Pkginfo $MPKGNAME32/Contents/"
run_cmd "$CMD"
CMD="cp -r -p $PKGNAME/Contents/Pkginfo $MPKGNAME64/Contents/"
run_cmd "$CMD"
CMD="cp -r -p $PKGNAME/Contents/Resources/Description.plist $MPKGNAME32/Contents/Resources/"
run_cmd "$CMD"
CMD="cp -r -p $PKGNAME/Contents/Resources/Description.plist $MPKGNAME64/Contents/Resources/"
run_cmd "$CMD"
CMD="cp -r -p $PKGNAME/Contents/Resources/ReadMe.rtf $MPKGNAME32/Contents/Resources/"
run_cmd "$CMD"
CMD="cp -r -p $PKGNAME/Contents/Resources/ReadMe.rtf $MPKGNAME64/Contents/Resources/"
run_cmd "$CMD"
CMD="cp -r -p $PKGNAME/Contents/Resources/background.tiff $MPKGNAME32/Contents/Resources/"
run_cmd "$CMD"
CMD="cp -r -p $PKGNAME/Contents/Resources/background.tiff $MPKGNAME64/Contents/Resources/"
run_cmd "$CMD"
CMD="cp -r -p $PKGNAME/Contents/Resources/License.rtf $MPKGNAME32/Contents/Resources/"
run_cmd "$CMD" 
CMD="cp -r -p $PKGNAME/Contents/Resources/License.rtf $MPKGNAME64/Contents/Resources/"
run_cmd "$CMD"  
CMD="cp -r -p $PKGNAME/Contents/Resources/background.tiff $MPKGNAME32/Contents/Resources/"
run_cmd "$CMD" 
CMD="cp -r -p $PKGNAME/Contents/Resources/background.tiff $MPKGNAME64/Contents/Resources/"
run_cmd "$CMD"
CMD="cp -r -p $PKGNAME/Contents/Resources/fr.lproj $MPKGNAME32/Contents/Resources/"
run_cmd "$CMD"
CMD="cp -r -p $PKGNAME/Contents/Resources/fr.lproj $MPKGNAME64/Contents/Resources/"
run_cmd "$CMD"
CMD="cp -r -p $PKGNAME/Contents/Resources/French.lproj $MPKGNAME32/Contents/Resources/"
run_cmd "$CMD"
CMD="cp -r -p $PKGNAME/Contents/Resources/French.lproj $MPKGNAME64/Contents/Resources/"
run_cmd "$CMD"


sed -e "s/ARCH/x86_64/g" $MPKGNAME64/Contents/Info.plist > $MPKGNAME64/Contents/Info.plist2
mv $MPKGNAME64/Contents/Info.plist2 $MPKGNAME64/Contents/Info.plist

sed -e "s/ARCH/i386/g" $MPKGNAME32/Contents/Info.plist > $MPKGNAME32/Contents/Info.plist2
mv $MPKGNAME32/Contents/Info.plist2 $MPKGNAME32/Contents/Info.plist


rm -rf "$STARTDIR/../build/kakola-i386-$VERSION.dmg"
CMD="hdiutil create $STARTDIR/../build/kakola-i386-$VERSION.dmg -srcfolder $MPKGNAME32/ -ov"
run_cmd "$CMD"
rm -rf "$STARTDIR/../build/kakola-x86_64-$VERSION.dmg"
CMD="hdiutil create $STARTDIR/../build/kakola-x86_64-$VERSION.dmg -srcfolder $MPKGNAME64/ -ov"
run_cmd "$CMD"


chown -R $CURRENTUSER:admin .

#hdid $STARTDIR/../build/kakola-$ARCH-$VERSION.dmg
#echo "installer -target / -pkg $STARTDIR/build/kakola-intel-$ARCH-$VERSION.mpkg"
#installer -target / -pkg $STARTDIR/../build/kakola-intel-$ARCH-$VERSION.mpkg

echo
echo
echo "Files bellow  have been generated :"
echo
echo "Directory : $STARTDIR/../build/"
echo
cd "$STARTDIR/../build/"
du -sh `basename "$MPKGNAME32"`
du -sh `basename "$MPKGNAME64"`
echo
echo
du -sh "kakola-i386-$VERSION.dmg"
du -sh "kakola-x86_64-$VERSION.dmg"
echo
echo
